package com.itheima.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.SetmealService;
import constant.RedisConstant;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import pojo.CheckGroup;
import pojo.CheckItem;
import pojo.Setmeal;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐服务
 */
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {
    
    @Autowired
    private SetmealDao setmealDao;
    
    @Autowired
    private CheckGroupDao checkGroupDao;
    
    @Autowired
    private CheckItemDao checkItemDao;
    
    @Autowired
    private JedisPool jedisPool;
    
    //添加套餐
    @Override
    public void add(Setmeal setmeal,Integer[] checkgropIds) {
        System.out.println("执行serviceImpl:add方法========");
        setmealDao.add(setmeal);
        //添加套餐和检查组的关联信息到关联表
        System.out.println("添加到mysql数据库完毕，图片id为：" + setmeal.getId());
        //添加成功，同时把图片保存在Redis
        if(setmeal.getImg() != null){
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        
            System.out.println("添加到redis成功，获取redis_db========:");
            System.out.println(jedis.smembers(RedisConstant.SETMEAL_PIC_DB_RESOURCES));
        }
        setSetmealAndCheckGroup(setmeal.getId(),checkgropIds);
    }
    
    //设置套餐和检查组关联关系
    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgropIds){
        //如果包含了检查组，就进行关联信息的添加
        if(checkgropIds != null && checkgropIds.length > 0){
            for(Integer checkgroupId : checkgropIds){
                Map<String, Integer> map = new HashMap<>();
                map.put("setmeal_id", setmealId);
                map.put("checkgroup_id", checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
    
    //分页条件查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页插件，在查询之前开启分页，查询后的数据自动分页
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //进行条件查询
        Page<Setmeal> setmeals = setmealDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }
    
    //查询当前套餐包含的检查组
    @Override
    public Integer[] findCheckgroupsWithSetmealId(Integer setmealId) {
        Integer[] checkgroupIds = setmealDao.findCheckgroupsWithSetmealId(setmealId);
        return checkgroupIds;
    }
    
    //删除
    @Override
    public void deleteById(Integer id) {
        setmealDao.deleteById(id);
    }
    
    //更新
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        System.out.println("serviceImpl edit接收到的setmeal图片和套餐名字");
        System.out.println(setmeal.getImg() + setmeal.getName());
        //更新套餐表信息
        setmealDao.edit(setmeal);
        //先删除管理表信息，再重新插入修改后的关联系
        setmealDao.deleteAssociation(setmeal.getId());
        //重新插入关联信息
        setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);
    }
    
    //获取所有套餐信息
    @Override
    public List<Setmeal> getAllSetmeal() {
        return setmealDao.getAllSetmeal();
    }
    
    //根据id找套餐，带检查组和检查项信息
    @Override
    public Setmeal findById(Integer id){
        Setmeal setmeal = setmealDao.findSetMealByIdWithCheckGroups(id);
        return setmeal;
    }
    
    //String:套餐名，Integer:预约数量
    @Override
    public List<Map<String, Integer>> findCountWithName() {
        List<Map<String, Integer>> list = setmealDao.findCountWithName();
        return list;
    }
    
    
}
